# Imperfect module to find the ICU library with cmake

find_program(ICU_CONFIG icu-config)

if(NOT ICU_CONFIG)
  set(ICU_FOUND FALSE)
else(NOT ICU_CONFIG)

  execute_process(COMMAND ${ICU_CONFIG} --exists
    RESULT_VARIABLE ICU_EXISTS_RESULT)
  if(NOT ${ICU_EXISTS_RESULT} EQUAL 0)
    set(ICU_FOUND FALSE)
  else(NOT ${ICU_EXISTS_RESULT} EQUAL 0)

    execute_process(COMMAND ${ICU_CONFIG} --cppflags-searchpath
      COMMAND sed s/^-I//g
      OUTPUT_VARIABLE ICU_INCLUDE_DIRS)

    execute_process(COMMAND ${ICU_CONFIG} --ldflags-libsonly
      COMMAND tr -d \n
      COMMAND sed "s/[	 ]*-l/ /g"
      COMMAND sed "s/^[	 ]*//"
      COMMAND sed "s/[	 ]*$//"
      OUTPUT_VARIABLE ICU_LIBRARY_NAMES)

    separate_arguments(ICU_LIBRARY_NAMES)

#    execute_process(COMMAND ${ICU_CONFIG} --ldflags-searchpath
#      COMMAND sed s/^-L//
#      OUTPUT_VARIABLE ICU_LIB_DIR)

    foreach(CURRENT_LIB_NAME ${ICU_LIBRARY_NAMES})
      set(CURRENT_LIBRARY ${CURRENT_LIB_NAME}-NOTFOUND)
      find_library(CURRENT_LIBRARY ${CURRENT_LIB_NAME})
      list(APPEND ICU_LIBRARIES ${CURRENT_LIBRARY})
    endforeach(CURRENT_LIB_NAME ${ICU_LIBRARY_NAMES})

    set(ICU_FOUND TRUE)

  endif(NOT ${ICU_EXISTS_RESULT} EQUAL 0)
endif(NOT ICU_CONFIG)

if(ICU_FOUND)
  if(NOT ICU_FIND_QUIETLY)
    MESSAGE(STATUS "Found ICU")
  endif(NOT ICU_FIND_QUIETLY)
else(ICU_FOUND)
  if(ICU_FIND_REQUIRED)
    MESSAGE(FATAL_ERROR "Could not find ICU")
  endif(ICU_FIND_REQUIRED)
endif(ICU_FOUND)

